#include "MCUIO.h"
#include "MCULEDDriver.h"
#include "MCUPhaser.h"
#include <math.h>
#include "MCUConfig.h"
#include "MCUUart.h"

//#define ADANDBUZ
#define USE_BUZ
extern u16 PinState;
// u16 adc_result_cap_buf[2] = {0};
// u16 adc_result_ntc__buf[2] = {0};

xdata short BuzTimeOut = 0;
xdata short BuzStopTimeOut = 0;
xdata short BuzTimeOutCount = 0;
xdata unsigned char BuzCnt = 0;

short Round(float x)
{
	return (short)(x + 0.5);
}

// void writeOut(char port, short byte, bool isOn)
// {
// 	HALwriteOut(port, byte, isOn);
// }

// u16 readOut(char port)
// {
// 	HALreadOut(port);
// }

// u16 readIn(char port)
// {
// 	HALreadIn(port);
// }

// extern TYPE_LL_ADC_SINGLE_CYCLE_INIT adc_init;
// // = {LL_ADC_ONCE_MD_CH_SEL_PB4, LL_ADC_ONCE_MD_CH_SEL_PC2};
// u16 getADC(char port)
// {
// #if (1 == MCU_CHIP_JUSHENG)

// 	adc_init.adc_dma_addr = (u32)&adc_result_buf[0];

// 	ll_adc_single_cycle_init(ADKEY, &adc_init);
// 	// ll_adc_single_cycle_init(ADKEY, &adc_init);
// 	/* start AD convert */
// 	ll_adc_start_enable();
// 	/* Waiting for AD convert completed   */
// 	while (!(LL_ADC_DONE_PENDING_GET()))
// 		;

// 	/* Clear AD convert flag              */
// 	LL_ADC_DONE_PENDING_CLR();
// 	switch (port)
// 	{
// 	case CAPCHANEL:
// 		return adc_result_buf[0];
// 	default:
// 		return 0;
// 	}

// #endif
// }

bool isBuzOn()
{
	return (bool)!!(PinState & S2PROTO_PIN_BUZ);
}

void EnableBuz()
{
	PinState |= S2PROTO_PIN_BUZ;
}

void DisableBuz()
{
	PinState &= ~S2PROTO_PIN_BUZ;
}

bool buzCtrl()
{
	if ((BuzCnt > 0) && (1 > BuzStopTimeOut) && (BuzTimeOutCount < BuzTimeOut))
	{
		// if (false == isBuzOn())
		{
			EnableBuz();
		}
	}
	else
	{
		if (true == isBuzOn())
		{
			DisableBuz();
			{
				if (BuzCnt > 0)
				{
					BuzCnt--;
					if (BuzCnt > 0)
					{
						BuzTimeOutCount = 0;
						BuzStopTimeOut = BuzTimeOut;
					}
				}
			}
		}
	}
	return isBuzOn();
}

void switchBuzOn(short timeout, char buzCnt)
{
	BuzCnt = buzCnt;
	BuzTimeOutCount = timeout/10;
}

void buzTimeMgr()
{
	if ((BuzCnt > 0) && (BuzStopTimeOut > 0))
	{
		BuzStopTimeOut--;
		BuzTimeOutCount = 0;
	}
	else if ((BuzCnt > 0) && (BuzStopTimeOut < 1) && (BuzTimeOutCount < BuzTimeOut))
	{
		BuzTimeOutCount++;
	}
}

float Powerf(float x, u8 m)
{
	float y = x;
	for (u8 i = 1; i < m; i++)
	{
		y *= x;
	}
	return y;
}
short DegreeToNTCVal(short x)
{
	if (x > 205)
	{
		// y = -0.0002182201 x3.0000000000  + 0.1305435996 x2.0000000000  - 5.1360365118 x + 80.7249850676
		return (short)(-0.0002182201 * x * x * x + 0.1305435996 * x * x - 5.1360365118 * x + 80.7249850676);
	}
	else if (x > 60)
	{
		return (short)(-0.0001316994 * x * x * x + 0.1035686904 * x * x - 2.9348198580 * x + 48.6133381892);
	}
	else
	{
		return (6.0021 * x - 99.446);
	}
	// float x = Temp - 20 * 1.8;

	// #ifdef USA_VERSION
	// 	//华氏度转摄氏度
	// 	float x = (Temp - 34) / 1.8;
	// 	//降温15度
	// 	// x -= 24;
	// 	x -= 24;
	// 	return (short)( -0.0001316994 * x * x * x  + 0.1035686904 * x * x  - 2.9348198580* x + 48.6133381892 );
	// #else
	// 	float x = Temp ;
	// 	return (short)( -0.0001316994 * x * x * x  + 0.1035686904 * x * x  - 2.9348198580* x + 48.6133381892 );
	// #endif
}

short NTCToDegree(short NTC)
{
	float x = 1.0 * NTC / 100.0;
	if (NTC < 433)
	{
		return Round(-2.5012 * Powerf(x, 5) + 294.72 * Powerf(x, 4) + 352.82 * Powerf(x, 3) + 262.7 * Powerf(x, 2) + 124.95 * x + 25.937);
	}
	else
	{
		return Round(0.00001852 * Powerf(x, 5) - 0.00174550 * Powerf(x, 4) + 0.06583929 * Powerf(x, 3) - 1.22623143 * Powerf(x, 2) + 14.89424138 * x + 23.37228975);
	}
}

#define TIME_GAP 4

#define NUM_TEMP 3
short TempRecord[NUM_TEMP] = {12, 12, 12};
void UpdateTemp(short currentTemp)
{
	TempRecord[NUM_TEMP - 1] = TempRecord[NUM_TEMP - 2];
	TempRecord[NUM_TEMP - 2] = TempRecord[NUM_TEMP - 3];
	TempRecord[NUM_TEMP - 3] = currentTemp;
}

void ClearTemp(short Temp)
{
	TempRecord[NUM_TEMP - 3] = TempRecord[NUM_TEMP - 2] = TempRecord[NUM_TEMP - 1] = Temp;
}

short TempChange = 0;
bool PIDCtrlTemp(short dstTemp, short currentTemp)
{
	UpdateTemp(currentTemp);
	TempChange = 0;
	if (TempRecord[0] > TempRecord[2])
	{
		//温度上升�?
		TempChange = TempRecord[0] - TempRecord[2];
		if ((currentTemp + TIME_GAP * (TempChange)) > dstTemp)
		{
			return false;
		}
		else
		{
			if (currentTemp < dstTemp)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		// return false;
	}
	else
	{
		//温度下降�?
		TempChange = TempRecord[2] - TempRecord[0];
		//注意溢出bug
		if (currentTemp < (dstTemp + TIME_GAP * (TempChange)))
		{
			return true;
		}
		else
		{
			if (currentTemp > dstTemp)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		// return false;
	}
}

short TempRecord1[NUM_TEMP] = {12, 12, 12};
void UpdateTemp1(short currentTemp)
{
	TempRecord1[NUM_TEMP - 1] = TempRecord1[NUM_TEMP - 2];
	TempRecord1[NUM_TEMP - 2] = TempRecord1[NUM_TEMP - 3];
	TempRecord1[NUM_TEMP - 3] = currentTemp;
}

void ClearTemp1(short Temp)
{
	TempRecord1[NUM_TEMP - 3] = TempRecord1[NUM_TEMP - 2] = TempRecord1[NUM_TEMP - 1] = Temp;
}

short TempChange1 = 0;
bool PIDCtrlTemp1(short dstTemp, short currentTemp)
{
	UpdateTemp1(currentTemp);
	TempChange1 = 0;
	if (TempRecord1[0] > TempRecord1[2])
	{
		//温度上升�?
		TempChange1 = TempRecord1[0] - TempRecord1[2];
		if ((currentTemp + TIME_GAP * (TempChange1)) > dstTemp)
		{
			return false;
		}
		else
		{
			if (currentTemp < dstTemp)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		// return false;
	}
	else
	{
		//温度下降�?
		TempChange1 = TempRecord1[2] - TempRecord1[0];
		//注意溢出bug
		if (currentTemp < (dstTemp + TIME_GAP * (TempChange1)))
		{
			return true;
		}
		else
		{
			if (currentTemp > dstTemp)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		// return false;
	}
}
